package com.nononsenseapps.feeder.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
   public static SimpleDateFormat SAMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");

   public static SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

   public static String toString(Date createdTime) {
       return DATE_TIME_FORMAT.format(createdTime);
   }

    public static final String timeAlong(Date createdTime) {
        if (createdTime != null) {
            long agoTimeInMin = (new Date(System.currentTimeMillis()).getTime() - createdTime.getTime()) / 1000 / 60;
            // 如果在当前时间以前一分钟内
            if (agoTimeInMin <= 1) {
                return "刚刚";
            } else if (agoTimeInMin <= 60) {
                // 如果传入的参数时间在当前时间以前10分钟之内
                return agoTimeInMin + "分钟前";
            } else if (agoTimeInMin <= 60 * 24) {
                return agoTimeInMin / 60 + "小时前";
            } else if (agoTimeInMin <= 60 * 24 * 15) {
                return agoTimeInMin / (60 * 24) + "天前";
            } else if (agoTimeInMin <= 60 * 24 * 15) {
                return agoTimeInMin / (60 * 24) + "天前";
            } else {
                return SAMPLE_DATE_FORMAT.format(createdTime);
            }
        } else {
            return SAMPLE_DATE_FORMAT.format(new Date(0));
        }
    }

    /**
     * 根据时间戳 返回发布时间距离当前的时间
     */
    public static String getTimeStampAgo(String timeStamp) {
        long time = Long.parseLong(timeStamp);
        String string = DATE_TIME_FORMAT.format(time * 1000L);
        Date date = null;
        try {
            date = DATE_TIME_FORMAT.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeAlong(date);
    }

    public static String getCurrentTimeStamp() {
        return String.valueOf(System.currentTimeMillis() / 1000);
    }

    public static final int COVENT_HOUR=0;
    public static final int COVENT_DAY=1;
    public static final int COVENT_THREE_DAY=2;
    public static final int COVENT_WEEK=3;
    public static final int COVENT_MONTH=4;

    public static Date covertFormCurrentDate(int type){
        return covertFormDate(type,Calendar.getInstance());
    }

    public static Date covertFormDate(int type, Calendar inDate){
        switch (type){
            case COVENT_HOUR:
                inDate.set(Calendar.MINUTE,0);
                inDate.set(Calendar.SECOND,0);
                break;
            case COVENT_DAY:
                inDate.set(Calendar.HOUR_OF_DAY,0);
                inDate.set(Calendar.MINUTE,0);
                inDate.set(Calendar.SECOND,0);
                break;
            case COVENT_THREE_DAY:
                inDate.set(Calendar.HOUR_OF_DAY,0);
                inDate.set(Calendar.MINUTE,0);
                inDate.set(Calendar.SECOND,0);
                inDate.add(Calendar.DAY_OF_MONTH,-2);
                break;
            case COVENT_WEEK:
                inDate.set(Calendar.HOUR_OF_DAY,0);
                inDate.set(Calendar.MINUTE,0);
                inDate.set(Calendar.SECOND,0);
                inDate.add(Calendar.DAY_OF_WEEK,0);
                break;
            case COVENT_MONTH:
                inDate.set(Calendar.HOUR_OF_DAY,0);
                inDate.set(Calendar.MINUTE,0);
                inDate.set(Calendar.SECOND,0);
                inDate.set(Calendar.DAY_OF_MONTH,0);
                break;
            default:
                break;
        }
        return inDate.getTime();
    }

}

